import { ref } from "vue";
import { http } from "../http";

export function useList(resource: string | undefined) {
  const page = ref({
    pageSize: 5,
    pageSizes: [5, 10, 15, 30, 50],
    total: 0,
  }) as any;

  const params = ref({
    size: page.value.pageSize,
    currentPage: 1,
  }) as any;
  const handleSizeChange = (pageSize: number) => {
    params.value.size = pageSize;
    getList();
  };
  const handleCurrentChange = (currentPage: number) => {
    params.value.currentPage = currentPage;
    getList();
  };

  const list = ref([]);
  const getList = async (status: string | null = null) => {
    ({ items: list.value, total: page.value.total } = (
      await http.get(`/${resource}`, {
        params: { ...params.value, status },
      })
    )?.data);
  };

  return {
    list,
    getList,
    page,
    handleSizeChange,
    handleCurrentChange,
  };
}
